package HttpHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class HttpPostHelper {
	
	
	
	
	public int postRegister(String IMEI,String email,String password ) {
	    // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
	    
	    String result;

	    try {
	        // Add your data
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	        nameValuePairs.add(new BasicNameValuePair("IMEI", IMEI));
	        nameValuePairs.add(new BasicNameValuePair("email", email));
	        nameValuePairs.add(new BasicNameValuePair("password", password));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        
	        
	        if (response != null) {

				InputStream in = response.getEntity().getContent(); // Get
				// the
				// data
				// in
				// the
				// entity
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(in, "UTF-8"), 8);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				result = sb.toString();
				System.out.println(result);
				
				
				if(result.equalsIgnoreCase("user_exists")){
					return 1;
				}
				
				if (result.equalsIgnoreCase("registered")){
					return 2;
				}

			}
	        
	        
		    return 0;

	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    }
	    return 0;
	} 
	
	
	
	
	public int RequestPassword(String username,String password,String IMEI){
		
		 HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
		    
		    String result;
		
		 try {
		        // Add your data
		        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		        nameValuePairs.add(new BasicNameValuePair("type","PasswordRequest"));
		        nameValuePairs.add(new BasicNameValuePair("IMEI", IMEI));
		        nameValuePairs.add(new BasicNameValuePair("username", username));
		       // nameValuePairs.add(new BasicNameValuePair("password", password));
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

		        // Execute HTTP Post Request
		        HttpResponse response = httpclient.execute(httppost);
		        
		        
		        while (response != null) {

					Thread.sleep(10000);
					response =    httpclient.execute(httppost);


				}
		        
		        InputStream in = response.getEntity().getContent(); // Get
				// the
				// data
				// in
				// the
				// entity
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(in, "UTF-8"), 8);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				result = sb.toString();
				System.out.println(result);
				
				
				if(result.equalsIgnoreCase("user_notfound")){
					return 1;
				}
				
				if (result.equalsIgnoreCase("user_found")){
					
					return 2;
				}

			
		        
		        
			    return 0;

		    } catch (ClientProtocolException e) {
		        // TODO Auto-generated catch block
		    } catch (IOException e) {
		        // TODO Auto-generated catch block
		    } catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    return 0;
		
		
	}
	
	
}
